var app = getApp();
Page({
  TOAPPID: require('../../resource/components/footer/footer.js'),
  /**
   * 页面的初始数据
   */
  data: {
    openid: '',
    show: false,
    wifiOpen: false,
    photoType: 0,
    res: app.siteInfo.resource,
    showPic: false,
    isAdv: false,
    pkey: 0,
    uploadPic: false,
    DOWN: '',
  },
  onShow: function () {
    var id = wx.getStorageSync('listId');
    if (id) {
      this.onLoad();
    }
  },
  onHide: function () {
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = wx.getStorageSync('listId');
    var that = this;
    app.footerInit(that);
    wx.showLoading();
    app.getShareData(function (res) { that.setData({ cfg: res.data }) });
    app.getUserInfo(function (openid) {
      that.setData({ openid: openid });
      wx.getUserInfo({
        success: function (wxInfo) {
          var info = JSON.parse(wxInfo.rawData);
          app.util.request({
            url: 'entry/wxapp/updateUser',
            data: { openid: openid, avatar: info.avatarUrl, nickname: info.nickName, gender: info.gender, m: 'junsion_wifinav' },
            cachetime: 0,
            success: function (res) {

            }
          });
        }
      });
      app.util.request({
        url: 'entry/wxapp/enter',
        data: { openid: openid, op: 'getPic2', m: 'junsion_wifinav' },
        cachetime: 0,
        success: function (res) {
          wx.setNavigationBarTitle({
            title: res.data.title ? res.data.title : '入驻小程序',
          })
          wx.hideLoading();
          var data = res.data;
          that.setData({ show: true });
          if (data.enter_4) {
            that.setData({ enter_1: data.enter_4 });
          }
          if (data.enter_5) {
            that.setData({ enter_2: data.enter_5 });
          }
          if (data.enter_6) {
            that.setData({ enter_3: data.enter_6 });
          }
          if (data.packages) {
            that.setData({ packages: data.packages });
          }
        }
      });
    });
    //存在id则编辑入驻信息
    if (id) {
      app.util.request({
        url: 'entry/wxapp/enter',
        data: { id: id, op: 'getApp', m: 'junsion_wifinav' },
        cachetime: 0,
        success: function (res) {
          if (res.data.code == 1) {
            wx.removeStorageSync('listId');
            that.setData({ app: res.data.app });
        
          } else {
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg,
              showCancel: false,
              success: function () {
                wx.navigateTo({ url: '../user/mylist2/index' });
              }
            });
          }
        }
      });
    } 
  },
  confirmSubmit: function (e) {
    var that = this;
    //编辑入驻信息
    var apps = that.data.app;
    var idx = that.data.idx;
    var isAdv = that.data.isAdv;
    var data = {
      realname: e.detail.value.realname,
      mobile: e.detail.value.mobile,
      des: e.detail.value.des,
      title: e.detail.value.title,
      isAdv: isAdv,
      m: 'junsion_wifinav',
      openid: that.data.openid
    }
    if (!data.realname) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入姓名',
        showCancel: false,
      });
      return;
    }
    if (!data.mobile) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入电话',
        showCancel: false,
      });
      return;
    }
    if (!data.title) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入小程序名字',
        showCancel: false,
      });
      return;
    }
    if (!data.des) {
      wx.showModal({
        title: '温馨提示',
        content: '请输入小程序描述',
        showCancel: false,
      });
      return;
    }
    if (apps) {
      data.id = app.id;
    } 

    //选则启用广告
    data.isAdv = 0;
    if (isAdv) data.isAdv = 1;
    if (!that.data.logo) {
        wx.showModal({
          title: '温馨提示',
          content: '请上传小程序LOGO',
          showCancel: false,
        });
        return;
      }
    if (!that.data.qrcode) {
        wx.showModal({
          title: '温馨提示',
          content: '请上传小程序二维码',
          showCancel: false,
        });
        return;
      }
      if (data.isAdv > 0) {
        if (!that.data.pkey) {
          wx.showModal({
            title: '温馨提示',
            content: '请选择套餐',
            showCancel: false,
          });
          return;
        }
        var pid = that.data.packages[that.data.pkey].id;
        if (!pid) {
          wx.showModal({
            title: '温馨提示',
            content: '请选择套餐',
            showCancel: false,
          });
          return;
        }
      }
      wx.showLoading({ title: '上传中...' });
      data.pid = pid;
      data.m = 'junsion_wifinav';
      wx.uploadFile({
        url: app.util.url('entry/wxapp/upload2'),
        filePath: that.data.logo,
        name: 'photo',
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        formData: data,
        success: function (res) {
          wx.hideLoading();
          res['data'] = JSON.parse(app.delBoM(res.data));
          if (res.data.code == 1 || res.data.code == 10 || res.data.code == 11){
            data = { m: 'junsion_wifinav', openid: that.data.openid };
            if (res.data.code == 10) data['oid'] = res.data.oid;
            else if(res.data.code == 11) data['aid'] = res.data.aid;
            wx.uploadFile({
              url: app.util.url('entry/wxapp/upload2'),
              filePath: that.data.qrcode,
              name: 'qrcode',
              header: { 'content-type': 'application/x-www-form-urlencoded' },
              formData: data,
            });
           if (res.data.code == 1) {
              wx.requestPayment({
                timeStamp: res.data.params.timeStamp,
                nonceStr: res.data.params.nonceStr,
                package: res.data.params.package,
                signType: res.data.params.signType,
                paySign: res.data.params.paySign,
                success: function (res) {
                  wx.showModal({
                    title: '温馨提示',
                    content: '支付成功',
                    showCancel: false,
                    success: function (r) {
                      wx.navigateTo({ url: '../user/mylist2/index' });
                    }
                  });
                }
              });
            } else if (res.data.code == 10) {
              wx.showModal({
                title: '温馨提示',
                content: '提交成功',
                showCancel: false,
                success: function (r) {
                  wx.navigateTo({ url: '../user/mylist2/index' });
                }
              });
            }
        } else {
            wx.showModal({
              title: '温馨提示',
              content: res.data.msg,
              showCancel: false,
            });
          } 
        }
      });
  },
  onShareAppMessage: function () {
    return {
      title: this.data.cfg.share.applyShare,
      path: '/sharing/pages/apply/apply',
      imageUrl: this.data.cfg.share.applyPic,
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  onPullDownRefresh: function () {
    this.onLoad();
    wx.stopPullDownRefresh();
  },
  imgView: function (e) {
    wx.previewImage({
      urls: [e.target.dataset.src],
    })
  },
  switchChange: function (e) {
    this.setData({ isAdv: e.detail.value });
  },
  clickPic: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        that.setData({ logo: res.tempFilePaths[0]});
      }
    })
  },
  clickPic2: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        that.setData({  qrcode: res.tempFilePaths[0] });
      }
    })
  },
  selPackage: function (e) {
    var idx = e.detail.value;
    this.setData({ pkey: idx });
  },
  onStaus: function () {
    var t = this.data.DOWN;
    if (t == 'slidedown') t = 'slideup';
    else t = 'slidedown';
    this.setData({ DOWN: t });
  }
})